package com.xyzk.diagnose.tools;

import java.io.IOException;
import java.io.InputStream;

public class ReadThread extends Thread{

	public  boolean isThreadValid = true;
	private InputStream  mInputStream;
	private ReadCallback mCallback;
	int buffer_size  = 1024;   //64
	
	public ReadThread(InputStream mInputStream){
		this.mInputStream = mInputStream;
	}
	
	@Override
	public void run() {
		     super.run();
		
			 int size;
			
			while(isThreadValid && !isInterrupted()){
				
				final byte[] buffer = new byte[buffer_size];
				if (mInputStream == null) return;
	
				try {
					size = mInputStream.read(buffer);
					if( size > 0){
						onReceivedData(buffer,size);	
					}
				} catch (IOException e) {
					e.printStackTrace();
				}	
			}

	}
	
	public void setCallBack(ReadCallback readCallback){
		   mCallback  = readCallback;
	}
	

    private void onReceivedData(byte[] data,int size){
        if( mCallback != null)
        	mCallback.onReceivedData(data,size);
    }
	
    // Read Callback
  public  interface ReadCallback{
        void onReceivedData(byte[] data,int size);
    }
}